package methodOne;

import java.util.ArrayList;
import java.util.Scanner;

public class UseMethod {
    public static void main(String[] args) {
        ArrayList<Student> list = new ArrayList<>();
        jiemian(list);
    }

    public static void jiemian(ArrayList<Student> list) {
        lo:
        while (true) {
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入下列序号：");
            System.out.println("1 添加学生信息");
            System.out.println("2 查询学生信息");
            System.out.println("3 修改学生信息");
            System.out.println("4 删除学生信息");
            System.out.println("5 退出使用");

            String s = scanner.nextLine();
            switch (s) {
                case "1":
                    addStudent(list);
                    break;
                case "2":
                    checkStudent(list);
                    break;
                case "3":
                    changeStudent(list);
                    break;
                case "4":
                    deletStudent(list);
                    break;
                case "5":
                    System.out.println("退出");
                    break lo;
                default:
                    System.out.println("你输入的数字有误！请重新输入！");


            }
        }

    }

    public static void addStudent(ArrayList<Student> list) {
        Scanner scanner = new Scanner(System.in);
        while (true){
            System.out.println("请输入学生的学号:");
            String id = scanner.next();
            int index=getindex(list,id);
            if (index==-1){
                //        student.setId(id);
                System.out.println("请输入学生的姓名:");
                String name = scanner.next();
//        student.setName(name);
                System.out.println("请输入学生的年龄:");
                int age = scanner.nextInt();
//        student.setAge(age);
                Student student = new Student(id, name, age);
                list.add(student);
                System.out.println("添加成功！");
                break;
            }else {
                System.out.println(id+"学号已存在，请重新输入！");
            }
        }

    }

    public static void changeStudent(ArrayList<Student> list) {
        if(list.size()==0){
            System.out.println("表为空！");
           return;
        }
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要修改学生信息对应的学号：");
        String id = scanner.next();
        int getindex = getindex(list, id);
        if (getindex == -1) {
            System.out.println(id + "该学号不存在！");
            return;
        }else {
            System.out.println("请输入要修改的姓名：");
            String name = scanner.next();
            list.get(getindex).setName(name);
            System.out.println("请输入要修改的年龄：");
            int age = scanner.nextInt();
            list.get(getindex).setAge(age);
            System.out.println(id + "..." + name + "..." + age + "修改成功！");
        }

    }

    public static void checkStudent(ArrayList<Student> list) {
        if (list.size() == 0) {
            System.out.println("无信息请先添加数据！");
            return;
        }
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i).getId() + "..." + list.get(i).getName() + "..." + list.get(i).getAge());
        }
    }

    public static int getindex(ArrayList<Student> list, String sid) {
        int index = -1;
        //遍历集合，获取每一个学生对象，准备查找
        for (int i = 0; i < list.size(); i++) {
            Student student = list.get(i);
            //获取每一个学生对象的学号
            String id = student.getId();
            //传入的学号和获取到的学号进行对比
            if (id.equals(sid)) {
                index = i;
            }
        }
        return index;
    }

    public static void deletStudent(ArrayList<Student> list){
        if(list.size()==0){
            System.out.println("表格为空！");
            return;
        }
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入被删除学生的学号：");
        String id=scanner.next();
        int getindex = getindex(list, id);
        if(getindex==-1){
            System.out.println(id+"学号不存在！");
            return;
        }else {
            System.out.println(list.get(getindex).getId()+"..."+list.get(getindex).getName()+"..."+list.get(getindex).getAge()+"已被删除");
            list.remove(getindex);
        }

    }
}
